home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
3_0
/
MTN_RENA
/
MTN_RENA.C
next >
Wrap
C/C++ Source or Header
|
1990-12-06
|
6KB
|
206 lines
/* what this thing does it rename all those old tech notes from the format
'000 about technotes' to the new way, 'TN.000.about technotes'. */
#include <HFS.h>
#include <MacTypes.h>
#include <StdFilePkg.h>
#include <ToolboxUtil.h>
#include <EventMgr.h>
main()
{
HParamBlockRec theFile;
/* for PBHGetFInfo */
int ioVRefNum = 0, /* VRefNum (Working Dir) */
ioFDirIndex = 1; /* search index for PBH */
long ioDirID = 0; /* No DirID! */
Str255 ioNamePtr, /* Name of file */
oldName; /* Original name of file */
OSErr theResult; /* result code */
Boolean CheckName();
int numFiles, /* number of files in selected directory */
i; /* counter */
CursHandle theCursor;
InitMac();
GetWD(&ioVRefNum); /* get working directory */
theCursor = GetCursor(watchCursor); /* get the watch cursor */
SetCursor(*theCursor); /* set it */
if (ioVRefNum != 0) /* if the user selected a directory */
{ /* setup Parm Block */
theFile.fileParam.ioCompletion = 0L;
theFile.fileParam.ioVRefNum = ioVRefNum;
theFile.fileParam.ioFDirIndex = 1;
theFile.fileParam.ioDirID = ioDirID;
theFile.fileParam.ioNamePtr = ioNamePtr;
/* count the number of files */
while ((PBHGetFInfo(&theFile, false) == 0))
{
numFiles = theFile.fileParam.ioFDirIndex++;
theFile.fileParam.ioDirID = 0; /* have to keep zero'ing this! */
theFile.fileParam.ioNamePtr[0] = 0; /* kill of the name, too */
}
theFile.fileParam.ioFDirIndex = 1; /* set index */
theFile.fileParam.ioDirID = 0; /* have to keep zero'ing this! */
theFile.fileParam.ioNamePtr[0] = 0; /* kill of the name, too */
for (i = 1; i<=numFiles; i++)
{
if (PBHGetFInfo(&theFile, false) == 0) /* while there's a file */
{
AppendString(oldName, 1, ioNamePtr, 1); /* save the old fileName */
if (CheckName(ioNamePtr)) /* see if it's an old technote, format 'nnn' */
{ /* if it is, return the new name, format 'TN.nnn.' */
if (ioNamePtr[0] > 31) /* if longer than 31 chars */
{
ioNamePtr[0] = 31; /* trunc it */
}
/* rename the file */
theResult = Rename(oldName, theFile.fileParam.ioVRefNum, ioNamePtr);
if (theResult != 0)
{
++theFile.fileParam.ioFDirIndex;
}
}
else
{
++theFile.fileParam.ioFDirIndex;
}
}
theFile.fileParam.ioNamePtr[0] = 0; /* kill the name (again!) */
theFile.fileParam.ioDirID = 0; /* the DirID too! */
}
}
SetCursor(&arrow); /* reset the cursor */
}
/* gets a working directory from the user.*/
GetWD(ioVRefNum)
int *ioVRefNum;
{
SFReply reply; /* reply record */
Point where; /* loc of dialog */
SFTypeList typeList; /* not used */
where.v = 50; /* any point */
where.h = 50;
SFGetFile(where, 0L, 0L, -1, typeList, 0L, &reply); /* put up SFGet */
if (reply.good == false) /* if cancel was hit */
{
*ioVRefNum = 0;
}
else /* selected! */
{
*ioVRefNum = reply.vRefNum;
}
return;
}
/* useless error handler. Basicaly waits until there are 50 errors and
exits. */
HandleError(theResult)
OSErr theResult;
{
static int counter = 0;
if (++counter>50)
{
ExitToShell();
}
}
/* checks theString to see if it is in the old format. Could be modified
to check anything, really. */
Boolean CheckName(theString)
Str255 theString;
{
Str255 theCopy, thePart;
int index = 1;
Boolean numbers;
theCopy[0] = 0; /* zero strings */
thePart[0] = 0;
while (index <= 3) /* length of header */
{
if ((theString[index] >= '0') && (theString[index] <= '9'))
{
thePart[index] = theString[index]; /* copy numbers */
thePart[0] = index; /* set length */
numbers = true;
}
else
{
numbers = false; /* nope */
break;
}
index++;
}
if (numbers) /* if it is a tn */
{ /* create the new string */
AppendString(theCopy, 1, "\pTN.", 1);
AppendString(theCopy, 4, thePart, 1);
AppendString(theCopy, 7, "\p.", 1);
AppendString(theCopy, 8, theString, 5);
AppendString(theString, 1, theCopy, 1); /* copy the copy into the original */
return true;
}
return false;
}
/* a useful string utility. It will copy a string into another, or copy
a part of a string into another one. Useful for building strings.
It will not, however, allow you to jam a string into the middle of
another a la munger.
theOriginal must be a Str255!
theCopy can be a non-variable ie: "\ptheCopy".
start is where to put theCopy in theOriginal.
start2 is where to start copying in theCopy.
An example from above: theCopy
AppendString(theCopy, 1, "\pTN.", 1); "\pTN."
AppendString(theCopy, 4, thePart, 1); "\pTN.000"
AppendString(theCopy, 7, "\p.", 1); "\pTN.000."
AppendString(theCopy, 8, theString, 5); "\pTN.000.Aboutâ•”"
*/
AppendString(theOriginal, start, theCopy, start2)
Str255 theOriginal,
theCopy;
int start,
start2;
{
while ((start2 <= theCopy[0]) && (start <=256)) /* make sure that:
start2 is in the valid part of the string,
start doesn't go past the length of 256 */
{
theOriginal[start++] = theCopy[start2++];
}
theOriginal[0] = start-1; /* set length byte */
}
InitMac()
{
InitGraf(&thePort);
InitFonts();
FlushEvents(everyEvent, 0);
InitWindows();
InitMenus();
TEInit();
InitDialogs(0L);
InitCursor();
}